﻿using System;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;

namespace NMA.Infrastructure.NHibernate.Tests.Repository
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;    

    [TestFixture]
    public class NewsRepositoryTest : TestingBase
    {
        [Test]
        public void Count_all_news_in_database()
        {
            int result = -1;

            if (NewsRepository != null)
            {
                result = NewsRepository.CountAllNews();

                Assert.IsTrue(result >= 0, "contain 2 item in database");
            }
        }

        [Test]
        public void Can_get_by_id_from_database()
        {
            INews entity = null;

            if (NewsRepository != null)
            {
                var obj = NewsList.FirstOrDefault<News>();

                entity = NewsRepository.GetById(obj.Id);

                Assert.IsNotNull(entity, "News is not null");
                Assert.IsTrue(entity.Id == obj.Id);
                Assert.IsTrue(entity.Title == obj.Title);
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == obj.CreatedDate.ToShortDateString());
            }
        }

        [Test]
        public void Can_get_by_news_in_database()
        {
            IEnumerable<INews> result = null;

            if (NewsRepository != null)
            {
                result = NewsRepository.GetBy(x => x.Title == "title 1");

                Assert.IsNotNull(result);

                var entity = result.FirstOrDefault<INews>();

                Assert.IsNotNull(entity);
                Assert.IsTrue(entity.Title == "title 1");
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == DateTime.Now.ToShortDateString());
            }
        }

        [Test]
        public void Can_update_news_in_database()
        {
            if (NewsRepository != null)
            {
                var entity = NewsList.FirstOrDefault<News>();

                var dbObj = NewsRepository.GetById(entity.Id);

                Assert.IsNotNull(dbObj);

                dbObj.Title = "update title";
                dbObj.UpdatedDate = DateTime.Now.AddDays(1);

                // update into database
                NewsRepository.Update(dbObj);

                // get out for assertion
                var updateObj = NewsRepository.GetById(dbObj.Id);

                Assert.IsNotNull(updateObj);
                Assert.IsTrue(updateObj.Id == dbObj.Id);
                Assert.IsTrue(updateObj.Title == "update title");
                Assert.IsTrue(updateObj.UpdatedDate.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString());
            }
        }

        [Test]
        public void Get_all_news_from_database_with_paging()
        {
            if (NewsRepository != null)
            {
                var result = NewsRepository.All(1, 10);

                Assert.IsTrue(result.TotalCount >= 2, "Total count");
                Assert.IsTrue(result.PageIndex == 1, "Page index");
                Assert.IsTrue(result.PageSize == 10, "Page size");
            }
        }
    }
}